package optimizers.simplega.report;

import java.awt.GridLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

import optimizers.simplega.Individual;

import visualization.GraphDisplayer;
import visualization.PlotExpData;

public class ReaderSGA {

	/**
	 * @param args
	 */
	
	public static void openReport(File file) {
		FileInputStream saveFile;
		try {
			saveFile = new FileInputStream(file);
			ObjectInputStream restore = new ObjectInputStream(saveFile);
			try {

				double[][] fitnessData = (double[][]) restore.readObject();
				double[][] templateData = (double[][]) restore.readObject();
				double[][] SDlogKData = (double[][]) restore.readObject();
				PlotExpData demo = new PlotExpData("Fitness over generations",
						fitnessData, new String[] { "Best 1", "Best 2",
								"Best 3", "Average" }, "Generation", "Fitness");
				PlotExpData demo1 = new PlotExpData(
						"Number of templates over generations",
						templateData,
						new String[] { "Best 1", "Best 2", "Best 3", "Average" },
						"Generation", "Number of templates");
				PlotExpData demo2 = new PlotExpData(
						"Standard deviation of logK over generations",
						SDlogKData, new String[] { "Best 1", "Best 2",
								"Best 3", "Average" }, "Generation",
						"Standard deviation of logK");
				JFrame graphFrame = new JFrame("Metrics over generations");
				graphFrame.setSize(1200, 800);
				GridLayout grid = new GridLayout(0, 1);
				JPanel panel = new JPanel();
				panel.setLayout(grid);
				grid.setColumns(2);
				graphFrame.add(panel);
				panel.add(demo);
				panel.add(demo1);
				panel.add(demo2);
				graphFrame.setVisible(true);

			} catch (Exception ex) {

			}

			
			try {
				String consoleText = (String) restore.readObject();
			} catch (Exception ex) {

			}
			
			try {
				ArrayList<Individual> individuals = (ArrayList<Individual>) restore.readObject();
				GraphDisplayer bestIndividuals = new GraphDisplayer(
						"Best Individual for each generations");
				for (Individual indiv : individuals){
					bestIndividuals.addGraph(indiv.getOligoModel()
							.getVisualization(250, 250), indiv
							.displayTimeSereis(), indiv
							.printModelInTextArea());
					bestIndividuals.display();
				}

			} catch (Exception ex) {
				ex.printStackTrace();
			}


			restore.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		JFileChooser fc = new JFileChooser();
		int returnVal;
		do {
			returnVal = fc.showOpenDialog(null);
		} while (returnVal != JFileChooser.APPROVE_OPTION
				&& returnVal != JFileChooser.CANCEL_OPTION);
		File file = fc.getSelectedFile();

		// File file = new File(
		// "E:\\java\\SB_Project\\result\\2013_01_04 14_30_48StructureMutationRate_0.5");
		System.out.println(file.toString());
		openReport(file);
	}
}
